package cn.ad.common;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Locale;

/**
 * @author dongbin.yu
 * @from 2016-06-13
 * @since V1.0
 */
public class CustomInternalResourceView extends InternalResourceView {

    @Override
    public boolean checkResource(final Locale locale) throws Exception {

        HttpServletRequest servletRequest = null;
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes instanceof ServletRequestAttributes) {
            servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
        }

        String filePath = servletRequest.getSession().getServletContext().getRealPath(getUrl());
        File file = new File(filePath);

        return file.exists();
    }
}
